package com.zzyl.service.impl;


import cn.hutool.json.JSONUtil;
import com.zzyl.base.ResponseResult;
import com.zzyl.constant.CacheConstants;
import com.zzyl.dto.FloorDto;
import com.zzyl.entity.Bed;
import com.zzyl.entity.Floor;
import com.zzyl.entity.Room;
import com.zzyl.mapper.FloorMapper;
import com.zzyl.mapper.RoomMapper;
import com.zzyl.service.BedService;
import com.zzyl.service.FloorService;
import com.zzyl.service.RoomService;
import com.zzyl.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.List;


@Service

public class FloorServiceImpl implements FloorService {


    @Autowired
    RoomService roomService;

    @Autowired
    BedService bedService;

    @Autowired
    private FloorMapper floorMapper;

    //增加楼层
    @Override
    public int addFloor(FloorDto floorDto) {
        Floor floor = new Floor();
        BeanUtils.copyProperties(floorDto, floor);
        return floorMapper.insert(floor);
    }



    //通过ID删除楼层
    @Override
    public int deleteFloor(Long id) {
        return floorMapper.deleteById(id);
    }



    //更新楼层信息
    @Override
    public int updateFloor(FloorDto floorDto) {
        Floor floor = new Floor();
        BeanUtils.copyProperties(floorDto, floor);
        floor.setUpdateTime(LocalDateTime.now());
        return floorMapper.updateById(floor);
    }


    //通过ID查询楼层信息
    @Override
    public FloorVo getFloor(Long id) {
        Floor floor = floorMapper.selectById(id);
        FloorVo floorVo = new FloorVo();
        BeanUtils.copyProperties(floor, floorVo);
        return floorVo;
    }

    //获取所有的楼层信息
    @Override
    public List<FloorVo> getAllFloors() {
       return floorMapper.selectAll();
    }

    @Override
    public List<FloorVo> getAllWithRoomAndBed() {
        return floorMapper.selectAllRoomAndBed();
    }

    @Override
    public List<FloorVo> selectAllByNur() {
        return floorMapper.selectAllByNur();
    }

    /**
     * 根据床位状态查询获取所有楼层数据
     *
     * @param status 床位状态{@link com.zzyl.enums.BedStatusEnum}
     * @return 所有楼层数据
     */
    @Override
    public List<FloorVo> getRoomAndBedByBedStatus(Integer status) {
        return floorMapper.getRoomAndBedByBedStatus(status);
    }

    @Override
    public List<FloorVo> getAllFloorsWithDevice() {
        HashSet<Integer> floor_number = new HashSet<>(); // 存储楼层号，自动去重
        /*
          binding_location----------------绑定位置
          location_type-------------------都为1 固定设备
          physical_location_type----------位置类型 0 楼层 1 房间 2 床位
         */

        // 从设备表中查出所有 绑定位置类型 为 1(房间)的数据
        List<Room> roomList = floorMapper.getRoomByDevice();

        // 查询设备表中位置类型为2(床位)的数据，拿到绑定位置，即为床号与床位表联查
        List<Bed> bedList = floorMapper.getRoomByBed();

        // 拿到这些信息包含了哪些楼层
        roomList.forEach(e -> floor_number.add(floorMapper.getFloorCodeByRoom(e.getFloorId())));

        bedList.forEach(e -> floor_number.add(floorMapper.getFloorCodeByBed(e.getRoomId())));

        // 根据楼层号返回楼层信息
        List<FloorVo> floorVoList = floorMapper.getFloorByCode(floor_number);


        return floorVoList;
    }


}

